﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using PCSComUtils.Common;
using PCSUtils.Admin;
using log4net.Config;

namespace PCSMain
{
    static class Program
    {
        private static bool _stateLogIn = true;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // configure log4net
            XmlConfigurator.Configure();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (_stateLogIn)
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(Constants.CULTURE_VN);
            }

            var thisForm = new MainForm();
            var frmLogin = new Login();

            frmLogin.ShowDialog();
            if (frmLogin.DialogResult == DialogResult.OK)
            {
                Application.Run(thisForm);
                _stateLogIn = true;
            }
            else
            {
                Application.Exit();
            }

            /*var needUpdate = HasNewVersion();
            if (needUpdate)
            {
                var processInfo = new ProcessStartInfo(Application.StartupPath + @"\PCSUpdater.exe", Constants.CULTURE_VN);
                Process.Start(processInfo);
                Process.GetCurrentProcess().Kill();
            }
            else
            {
                
            }*/
        }

        private static bool HasNewVersion()
        {
            var fileName = Application.StartupPath + @"\AutoUpdate.config";
            if (!File.Exists(fileName))
                return false;
            // read auto update configuration
            var autoUpdateConfig = new XmlDocument();
            autoUpdateConfig.Load(fileName);
            if (autoUpdateConfig.DocumentElement == null)
                return false;
            var serverPathNode = autoUpdateConfig.DocumentElement.SelectSingleNode("serverPath");
            // try to reach update server
            if (serverPathNode == null)
                return false;

            var serverConfigFile = serverPathNode.InnerText.Trim() + @"\ServerConfiguration.config";
            if (!File.Exists(serverConfigFile))
                return false;

            var serverConfig = new XmlDocument();
            serverConfig.Load(serverConfigFile);
            if (serverConfig.DocumentElement == null)
                return false;

            // new version from server
            var newVersion = new Version(serverConfig.DocumentElement.SelectSingleNode("newVersion").InnerText.Trim());
            // get current application version
            var lastUpdateVersion = autoUpdateConfig.DocumentElement.SelectSingleNode("currentVersion").InnerText.Trim();
            var currentVersion = !string.IsNullOrEmpty(lastUpdateVersion)
                                     ? new Version(lastUpdateVersion)
                                     : Assembly.GetExecutingAssembly().GetName().Version;
            // update new files then restart system
            return currentVersion.CompareTo(newVersion) < 0;
        }
    }
}
